package com.hqd.ch03.v50.beans.factory.support;

import com.hqd.ch03.utils.ObjectUtils;
import com.hqd.ch03.v50.config.ConstructorArgumentValues;
import com.hqd.ch03.v50.config.MutablePropertyValues;

public class ChildBeanDefinition extends AbstractBeanDefinition {


    private String parentName;


    /**
     * Create a new ChildBeanDefinition for the given parent, to be
     * configured through its bean properties and configuration methods.
     *
     * @param parentName the name of the parent bean
     * @see #setBeanClass
     * @see #setScope
     * @see #setConstructorArgumentValues
     * @see #setPropertyValues
     */
    public ChildBeanDefinition(String parentName) {
        super();
        this.parentName = parentName;
    }

    /**
     * Create a new ChildBeanDefinition for the given parent.
     *
     * @param parentName the name of the parent bean
     * @param pvs        the additional property values of the child
     */
    public ChildBeanDefinition(String parentName, MutablePropertyValues pvs) {
        super(null, pvs);
        this.parentName = parentName;
    }

    /**
     * Create a new ChildBeanDefinition for the given parent.
     *
     * @param parentName the name of the parent bean
     * @param cargs      the constructor argument values to apply
     * @param pvs        the additional property values of the child
     */
    public ChildBeanDefinition(
            String parentName, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {

        super(cargs, pvs);
        this.parentName = parentName;
    }

    /**
     * Create a new ChildBeanDefinition for the given parent,
     * providing constructor arguments and property values.
     *
     * @param parentName the name of the parent bean
     * @param beanClass  the class of the bean to instantiate
     * @param cargs      the constructor argument values to apply
     * @param pvs        the property values to apply
     */
    public ChildBeanDefinition(
            String parentName, Class<?> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {

        super(cargs, pvs);
        this.parentName = parentName;
        setBeanClass(beanClass);
    }

    /**
     * Create a new ChildBeanDefinition for the given parent,
     * providing constructor arguments and property values.
     * Takes a bean class name to avoid eager loading of the bean class.
     *
     * @param parentName    the name of the parent bean
     * @param beanClassName the name of the class to instantiate
     * @param cargs         the constructor argument values to apply
     * @param pvs           the property values to apply
     */
    public ChildBeanDefinition(
            String parentName, String beanClassName, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {

        super(cargs, pvs);
        this.parentName = parentName;
        setBeanClassName(beanClassName);
    }

    /**
     * Create a new ChildBeanDefinition as deep copy of the given
     * bean definition.
     *
     * @param original the original bean definition to copy from
     */
    public ChildBeanDefinition(ChildBeanDefinition original) {
        super(original);
    }

    @Override

    public String getParentName() {
        return this.parentName;
    }

    @Override
    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

    @Override
    public void validate() throws BeanDefinitionValidationException {
        super.validate();
        if (this.parentName == null) {
            throw new BeanDefinitionValidationException("'parentName' must be set in ChildBeanDefinition");
        }
    }


    @Override
    public AbstractBeanDefinition cloneBeanDefinition() {
        return new ChildBeanDefinition(this);
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof ChildBeanDefinition)) {
            return false;
        }
        ChildBeanDefinition that = (ChildBeanDefinition) other;
        return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other));
    }

    @Override
    public int hashCode() {
        return ObjectUtils.nullSafeHashCode(this.parentName) * 29 + super.hashCode();
    }

    @Override
    public String toString() {
        return "Child bean with parent '" + this.parentName + "': " + super.toString();
    }

}
